<template>
  <div class="home-category">
    <ul class="menu">
      <li v-for="item in categoryList" :key="item.id">
        <RouterLink :to="`/category/${item.id}`">{{item.name}}</RouterLink>
        <a v-for="sub in item.children" :key="sub.id" href="javascript:;">{{sub.name}}</a>
        <!-- 加载中（前）加骨架效果 -->
        <template v-if="!item.children">
          <XtxSkeleton width="70px" height="18px" bg="rgba(255,255,255,0.2)"></XtxSkeleton>
          <XtxSkeleton width="40px" height="18px" bg="rgba(255,255,255,0.2)"></XtxSkeleton>
        </template>

        <!-- 弹层 -->
        <div class="layer">

          <h4>{{item.brands?"品牌推荐":"分类推荐"}} <small>根据您的购买或浏览记录推荐</small></h4>
          <!-- 品牌的弹层 -->
          <ul v-if="item.brands">
            <li class="brands" v-for="pro in item.brands" :key="pro.id">
              <RouterLink to="/">
                <img :src="pro.picture" alt="" />
                <div class="info">
                  <p class="name ellipsis-2">
                    {{pro.name}}
                  </p>
                  <p class="desc ellipsis">{{pro.desc}}</p>
                  <!-- <p class="price"><i>¥</i>{{pro.price}}</p> -->
                </div>
              </RouterLink>
            </li>
          </ul>

          <!--  -->
          <ul v-else>
            <li v-for="pro in item.goods" :key="pro.id">
              <RouterLink to="/">
                <img :src="pro.picture" alt="" />
                <div class="info">
                  <p class="name ellipsis-2">
                    {{pro.name}}
                  </p>
                  <p class="desc ellipsis">{{pro.desc}}</p>
                  <p class="price"><i>¥</i>{{pro.price}}</p>
                </div>
              </RouterLink>
            </li>
          </ul>
        </div>

      </li>
    </ul>
  </div>
</template>

<script>
import { computed, reactive, onMounted } from 'vue'
import { useStore } from 'vuex'
// 导入获取品牌内容的api
import { getBrandAPI } from '@/api/category.js'
export default {
  name: 'HomeCategory',
  setup() {
    // 功能二:品牌
    // 现有计算属性中没有品牌数据,定义品牌数据(响应式的)
    // 计算属性同时依赖品牌数据,得到10个长度的数组
    // 定义api函数,组件调用,获取数据,设置给品牌数据
    // 加入品牌的结构和样式,渲染即可
    const brand = reactive({
      name: '品牌',
      children: [{ name: '品牌推荐' }],
      //   品牌推荐,请求后添加数据
      brands: []
    })
    onMounted(async () => {
      const data = await getBrandAPI(6)
      // console.log(data)
      brand.brands = data.result
    })

    //1.功能一:渲染
    //2.获取数据在vuex中存储
    //3.通过children有很多项,渲染需要的数据值
    //4.通过计算属性渲染
    const store = useStore()
    const categoryList = computed(() => {
      // vuex数据{{id,name,chilren:7}}
      const list = store.state.category.list.map(item => {
        const { id, name, goods, children } = item
        // 考虑没有数据的情况
        return { id, name, goods, children: children && children.slice(0, 2) }
      })
      list.push(brand)
      return list
    })

    return { categoryList }
  }
}
</script>

<style scoped lang="less">
.home-category {
  width: 250px;
  height: 500px;
  background: rgba(0, 0, 0, 0.8);
  position: relative;
  z-index: 99;
  .menu {
    > li {
      padding-left: 40px;
      height: 50px;
      line-height: 50px;
      &:hover {
        background: var(--xtx-color);
      }
      .layer {
        width: 990px;
        height: 500px;
        background: rgba(255, 255, 255, 0.8);
        position: absolute;
        left: 250px;
        top: 0;
        display: none;
        padding: 0 15px;
        h4 {
          font-size: 20px;
          font-weight: normal;
          line-height: 80px;
          small {
            font-size: 16px;
            color: #666;
          }
        }
        ul {
          display: flex;
          flex-wrap: wrap;
          li {
            width: 310px;
            height: 120px;
            margin-right: 15px;
            margin-bottom: 15px;
            border: 1px solid #eee;
            border-radius: 4px;
            background: #fff;
            &:nth-child(3n) {
              margin-right: 0;
            }
            a {
              display: flex;
              width: 100%;
              height: 100%;
              align-items: center;
              padding: 10px;
              &:hover {
                background: #e3f9f4;
              }
              img {
                width: 95px;
                height: 95px;
              }
              .info {
                padding-left: 10px;
                line-height: 24px;
                width: 190px;
                .name {
                  font-size: 16px;
                  color: #666;
                }
                .desc {
                  color: #999;
                }
                .price {
                  font-size: 22px;
                  color: var(--price-color);
                  i {
                    font-size: 16px;
                  }
                }
              }
            }
          }
          li.brands {
            height: 180px;
            a {
              align-items: flex-start;
              img {
                width: 120px;
                height: 160px;
              }
              .info {
                p {
                  margin-top: 8px;
                }
                .place {
                  color: #999;
                }
              }
            }
          }
        }
      }
      &:hover {
        .layer {
          display: block;
        }
      }
      a {
        margin-right: 4px;
        color: #fff;
        &:first-child {
          font-size: 16px;
        }
      }
    }
  }
}
</style>